export class AnysdkMgr{
    _isCapturing = false;
    ANDROID_API:string;
    IOS_API: string;

    init(){
        this.ANDROID_API = 'com/babykylin/NativeAPI';
        this.IOS_API = 'AppController';
    }

    /**获取手机电量 */
    getBatteryPercent(){
       if(cc.sys.isNative){
           if(cc.sys.os == cc.sys.OS_ANDROID){
               return jsb.reflection.callStaticMethod(this.ANDROID_API, 'getBatteryPercent', '()F');
           }
           else if(cc.sys.os == cc.sys.OS_IOS){
               return jsb.reflection.callStaticMethod(this.IOS_API, 'getBatteryPercent');
           }
           return 0.9;
       } 
    }

    login(){
        if(cc.sys.os == cc.sys.OS_ANDROID){
            jsb.reflection.callStaticMethod(this.ANDROID_API, 'Login', '()V');
        }
        else if(cc.sys.os == cc.sys.OS_IOS){
            jsb.reflection.callStaticMethod(this.IOS_API, 'login');
        }
        else{
            console.log('平台：' + cc.sys.os + '不能实现微信登陆功能！');
        }
    }

    /**
     * 原声的分享功能  
     * @param title 分享的标题
     * @param desc 分享的文字内容
     */
    share(title, desc){
        if(cc.sys.os == cc.sys.OS_ANDROID){
            jsb.reflection.callStaticMethod(this.ANDROID_API, 'Share', '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V', cc.vv.SI.appweb, title, desc);
        }
        else if(cc.sys.os == cc.sys.OS_ANDROID){
            jsb.reflection.callStaticMethod(this.IOS_API, 'share:shareTitle:shareDesc:', cc.vv.SI.appweb, title, desc);
        }
        else{
            console.log('平台：' + cc.sys.os + '不能实现分享功能！');
        }
    }

    /**截屏分享 */
    shareResult(){
        if(this._isCapturing){
            return;
        }
        this._isCapturing = true;
        let size = cc.director.getWinSize();
        let currentDate = Date.now();
        let filename = 'result_share.jpg';
        let fullPath = jsb.fileUtils.getWritablePath() + filename;
        if(jsb.fileUtils.isFileExist(fullPath)){
            jsb.fileUtils.removeFile(fullPath);
        }

        let texture = new cc.RenderTexture(Math.floor(size.width), Math.floor(size.height));
        texture.setPosition(cc.p(size.width/2, size.height/2));
        texture.begin();
        cc.director.getRunningScene().visit();
        texture.end();
        texture.saveToFile(filename, cc.ImageFormat.JPG);

        let tryTimes = 0;
        let fn = () => {
            if(jsb.fileUtils.isFileExist(fullPath)){
                let height = 100;
                let scale = height / size.height;
                let width = Math.floor(size.width * scale);

                if(cc.sys.os == cc.sys.OS_ANDROID){
                    jsb.reflection.callStaticMethod(this.ANDROID_API, '(Ljava/lang/String;II)V', fullPath, width, height);
                }
                else if(cc.sys.os == cc.sys.OS_IOS){
                    jsb.reflection.callStaticMethod(this.IOS_API, 'shareIMG:width:height:', fullPath, width, height);
                }
                else{
                    console.log('平台：' + cc.sys.os + '不支持分享功能！');
                }
                this._isCapturing = false;
            }
            else{
                tryTimes++;
                if(tryTimes > 10){
                    console.log('分享超时！');
                    return;
                }
                setTimeout(fn, 50);
            }
        };
        setTimeout(fn, 50);
    }

    onLoginResp(code){
        let fn = (ret:cc.ret) => {
            if(ret.errcode == 0){
                cc.sys.localStorage.setItem('wx_account', ret.account);
                cc.sys.localStorage.setItem('wx_sign', ret.sign);
            }
            cc.vv.userMgr.onAuth(ret);
        };
        let obj = {
            code,
            os: cc.sys.os
        };
        cc.vv.http.sendRequest('/wechat_auth', obj, fn);
    }
}